<?php
/**
 * Introduction: 商品评价逻辑
 * @author: 杨陈鹏
 * @date: 2016/4/24 14:11
 * @email: yangchenpeng@cdlinglu.com
 */

namespace Common\Logic;


use Common\Model;

class GoodsCommentLogic
{

    static public $model,$model_replay;

    public function __construct()
    {

        self::$model = new Model\GoodsCommentModel();

        self::$model_replay = new Model\GoodsCommentReplyModel();

    }
    /**
     * Introduction: 获取评论列表
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function getData($where, $fileds = '*', $order = 'create_time desc', $page = 1, $nums = 20)
    {

        //return $where;
        //if(!$where)
        ///    return ['code' => 300 ,'msg' => '参数错误!'];

        $data = self::$model
                        //->fetchSql(true)
                        ->where($where)
                        ->field($fileds)
                        ->order($order)
                        ->page($page,$nums)
                        ->select();

        $total = self::$model
            //->fetchSql(true)
            ->where($where)
            ->count();

        if(!$data)
            return ['code' => 200, 'msg' => '暂无数据!'];

        $count['total'] = self::$model
                            ->where( ['goods_id' => $where['goods_id']] )
                            ->count();
        $count['type_1'] = self::$model
                            ->where( ['goods_id' => $where['goods_id'], 'type' => 1] )
                            ->count();
        $count['type_2'] = self::$model
                            ->where( ['goods_id' => $where['goods_id'], 'type' => 2] )
                            ->count();
        $count['type_3'] = self::$model
                            ->where( ['goods_id' => $where['goods_id'], 'type' => 3] )
                            ->count();

        $goods_logic = D('Common/Goods','Logic');

        $user_logic = D('Common/Member','Logic');



        foreach($data as $k => $v)
        {

            $reply_data = self::$model_replay
                                ->where(['goods_comment_id' => $v['id']])
                                ->order('create_time asc')
                                ->select();

            $data[$k]['images'] = $data[$k]['images'] ? unserialize($data[$k]['images']) : [];

            //获取商品信息
            $goods_info = $goods_logic::$model
                                ->where(['id' => $v['goods_id']])
                                ->field('title,thumb')
                                ->find();

            $data[$k]['goods_title'] = $goods_info['title'];
            $data[$k]['goods_thumb'] = $goods_info['thumb'];
            $data[$k]['create_time'] = date('Y-m-d H:i:s');

            //获取评论人信息

            $data[$k]['user_info'] = $user_logic::$model
                                                ->where(['uid' => $v['uid']])
                                                ->field('avatar,username,nicname')
                                                ->find();

            if($reply_data)
               $data[$k]['reply'] = $reply_data;

        }

        return ['code' => 200, 'data' => $data,'count' => $count,'total' => $total];



    }
    /**
     * Introduction: 获取条数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function getOneData($comment_id, $fileds = '*')
    {

        if(!$comment_id)
            return ['code' => 300 , 'msg' => '参数错误!'];

        $data = self::$model
                        ->where(['id' => $comment_id])
                        ->field($fileds)
                        ->find();

        $replay = self::$model_replay
                        ->where(['goods_comment_id' => $data['goods_comment_id']])
                        ->order('create_time desc')
                        ->select();

        if(!$replay)
            $data['reply'] = $replay;

        return ['code' => 200, 'data' => $data];

    }
    /**
     * Introduction: 发表评论
     * @author: 杨陈鹏
     * @date: 2016/4/24 14:11
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function create($data,$order_id,$uid)
    {

        if (!$uid)
            return ['code' => 500 ,'msg' => '请登录!'];

        if (!$order_id)
            return ['code' => 300 ,'msg' => '缺少订单信息!'];

        $goods_ids = [];

        //是否分配优惠卷

        $is_volume = false;

        foreach ($data as $k => $v)
        {

            if (!$v['goods_id'])
                return ['code' => 300,'msg' => '缺少商品信息!'];

            //return ['length' => strlen($v['content']),'conent' => $v['content']];
            if (strlen($v['content']) < 5)
                return ['code' => 300,'msg' => '评论内容至少5个字符以上!'];

            if(!$v['star'])
                return ['code' => 300, 'msg' => '请为商品打星'];

            if(!$v['type'])
                return ['code' => 300, 'msg' => '请选择商品评价'];

            $goods_ids[] = $v['goods_id'];

            $datas[$k]['uid'] = $uid;
            $datas[$k]['goods_id'] = $v['goods_id'];
            $datas[$k]['images'] = $v['images'] ? serialize($v['images']) : '' ;
            $datas[$k]['create_time'] = NOW_TIME;
            $datas[$k]['content'] = $v['content'];
            $datas[$k]['star'] = $v['star'];
            $datas[$k]['type'] = $v['type'];

            //如果有晒图,则给分配优惠卷
            if( $v['images'] )
                $is_volume = true;

        }

        //return self::$model->fetchSql(true)->addAll($datas);

        if(self::$model->addAll($datas)){

            //评论成功,商品的评论数量加1

            $goods_logic = D('Common/Goods','Logic');

            $ids = implode(',',$goods_ids);

            $goods_logic::$model->where("id IN($ids)")->setInc('comment',1);

            //更新订单信息

            $order_logic = D('Common/Order','Logic');

            $order_logic::$model
                            ->where(['id' => $order_id])
                            ->setField(['status' => 99,'complete_time' => NOW_TIME]);


            //计算商品的星级

            foreach($goods_ids as $v)
            {

                $avg_star = self::$model
                            ->where(['goods_id' => $v])
                            ->avg('star');

                $goods_logic::$model
                    ->where(['id' => $v])
                    ->setField('goods_star',$avg_star);


            }

            //领取优惠卷
            $volume_logic = new VolumeLogic();
            //如果当前订单的评论已经领取过,则不能再领取
            if(!$volume_logic::$model->where(['order_id' => $order_id])->count() && $is_volume){

                $code       =   strtolower(create_randomstr(8));
                    re:
                        $code = strtolower(create_randomstr(8));

                if($volume_logic::$model->where(['code' => $code])->count())
                    goto re;

                $result = $volume_logic::create(['uid' => $uid,'order_id' => $order_id,'code' => $code]);

                if ( $result ){
                    return ['code' => 200, 'msg' => '评论成功,系统赠送一张优惠卷!'];
                } else {
                    return ['code' => 200, 'msg' => '评论成功!'];
                }


            }

            return ['code' => 200, 'msg' => '评论成功!'];

        }

        return ['code' => 300, 'msg' => '评论失败'];



    }

    /**
     * Introduction: 回复评论
     * @author: 杨陈鹏
     * @date: 2016/4/24 14:11
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function reply($data)
    {

        if(!$data['goods_comment_id'])
            return ['code' => 300 ,'msg' => '缺少评论ID!'];

        if(!$data['content'])
            return ['code' => 300 ,'msg' => '回复内容不能为空!'];

        $datas['create_time'] = NOW_TIME;

        $datas['goods_comment_id'] = $data['goods_comment_id'];
        $datas['content'] = $data['content'];

        //return $datas;

        //return self::$model_replay->fetchSql(true)->add($datas);

        if(self::$model_replay->add($datas))
            return ['code' => 200, 'msg' => '回复成功!'];

        return ['code' => 300, 'msg' => '回复失败!'];



    }

    /**
     * Introduction: 评论点赞
     * @author: 杨陈鹏
     * @date: 2016/4/24 14:11
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function like($comment_id)
    {

        if(!$comment_id)
            return ['code' => 300, 'msg' => '缺少评论信息'];

        if(self::$model->where(['id' => $comment_id])->setInc('likes'))
        {

            $likes = self::$model->where(['id' => $comment_id])->getField('likes');

            return ['code' => 200, 'msg' => '操作成功!', 'data' => ['likes' => $likes]];
        }


        return ['code' => 300, 'msg' => '操作失败!'];

    }




}